package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO getData(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    @Override//查询套餐总览
    public SetmealOverViewVO getsetmeals() {
        //查询全部套餐
        List<Setmeal>setmealList= setmealMapper.getsetmeals();
        //套餐启售数量
        Integer sellCount=0;
        //套餐停售数量
        Integer removeCount=0;
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus()==1){
                sellCount++;
            }else if (setmeal.getStatus()==0){
                removeCount++;
            }
        }
        SetmealOverViewVO setmealOverViewVO=SetmealOverViewVO.builder()
                .sold(sellCount)
                .discontinued(removeCount)
                .build();
        return setmealOverViewVO;

    }

    @Override//查询菜品总览
    public DishOverViewVO getdishes() {
        //查询全部的菜品
        List<Dish>dishList=dishMapper.getdishes();
        //菜品启售数量
        Integer startSale=0;
        //菜品停售数量
        Integer stopSale=0;
        for (Dish dish : dishList) {
            if (dish.getStatus()==1) {
                startSale++;
            }else if (dish.getStatus()==0){
                stopSale++;
            }
        }
        DishOverViewVO dishOverViewVO=DishOverViewVO
                .builder()
                .sold(startSale)
                .discontinued(stopSale)
                .build();
        return dishOverViewVO;

    }

    @Override//查询订单管理数据
    public OrderOverViewVO getOrders() {
        //查询全部订单
        Integer orders=orderMapper.getAllOrders();
        //待接单数
        Integer Pendingorders = orderMapper.getstatus(Orders.TO_BE_CONFIRMED);
        //派送单数
        Integer Delivery = orderMapper.getstatus(Orders.DELIVERY_IN_PROGRESS);
        //已完成单数
        Integer Completed = orderMapper.getstatus(Orders.COMPLETED);
        //已取消单数
        Integer Cancelled = orderMapper.getstatus(Orders.CANCELLED);
        OrderOverViewVO orderOverViewVO=OrderOverViewVO
                .builder()
                .cancelledOrders(Cancelled)
                .completedOrders(Completed)
                .waitingOrders(Pendingorders)
                .deliveredOrders(Delivery)
                .allOrders(orders)
                .build();
        return orderOverViewVO;
    }

}




